% simple symnmf run
% by Sun Sibai

addpath('./jsonlab'); % using JSONLab (http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave)

datas={'1080598.txt'};
clsts=2:20;
%datas={'1080598.txt';'1141410.txt';'502309.txt';'1141261.txt'};
if (exist('out','dir')==0)
 mkdir('out');
end
for str=1:size(datas,1)
 str=cell2mat(datas(str));
 X=importfile(str);
 A=swloadsp(X);
 M=A*A'+A'*A;
 W=swnormal(M);
 IdxTree = zeros(size(A,1),2);
 IdxTree(:,1) = 1:size(A,1);
 FlowRec = zeros(19,2);
 for c=clsts
  [H,iter,err]=symnmf_anls(W,c,struct('debug',2));
  H=full(H);
  [cl,u]=sort(sum(H));
  Hs=zeros(size(H));
  for it=1:size(u,2)
   Hs(:,it)=H(:,u(it));
  end
  [Max,Idx]=max(abs(Hs'));
  Idx=Idx+20*(Max==0);
  Idx=Idx';
  IdxTree(:,2) = Idx;
  FlowRec(c,:)=[c,sum(sum(swflow(A,IdxTree).^2))];
  save(strcat('out/',str,'.c',sprintf('%d',c),'.IdxTCom.txt'),'IdxTree','-ascii');
  save(strcat('out/',str,'.c',sprintf('%d',c),'.FlowCom.txt'),'FlowRec','-ascii');
 end
end

